<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
header("Content-Type:text/html;charset=utf-8");

class Fetion{
    private $userAgent  = 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16';
    private $t          = null;
    private $user       = null;
    private $pwd        = null;
    private $status     = 4;
    private $login      = null;
    private $jsessionid = null;
    private $fix        = null;

    function  __construct($user, $pwd, $tatus) {
        $this->user     = $user;
        $this->pwd      = $pwd;
        $this->status   = $tatus;
        $this->login($this->user, $this->pwd, $this->status);
        
    }
    //登陆
    function login($mobile,$pwd,$loginstatus=4) {
        
        $url    = "http://f.10086.cn/im/login/inputpasssubmit1.action";
        $ch = curl_init();
		$post_data	= "loginstatus={$this->status}&m={$this->user}&pass={$this->pwd}";
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //这行是设定curl是否跟随header发送的location, 重要
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_REFERER, 'http://f.10086.cn/im/');
        curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        $return = curl_exec($ch);
        if(!preg_match('/中国移动WAP飞信/', $return)) {
            echo json_encode(array('code'=>'error','msg' =>'登陆失败'));
            exit;
        }
        curl_close($ch);
        preg_match('/ontimer="(.*?)"/', $return, $re);
        
        $parse      = parse_url($re[1]);
        $t          = str_replace('t=', '', $parse['query']);
        preg_match('~jsessionid=(.*?)\?~', $re[0], $ss);
        //初始化参数变量
        $this->jsessionid   = $ss[1];
        $this->t    = $t;
        $this->fix  = ';jsessionid='.$this->jsessionid.'?t='.$this->t;
        $this->login    = array('go_url' =>$re[1], 'url' => $url, 't'=>$t,'jsessionid'=>$this->jsessionid);
    }
    
    /**
     * 手机号码转换飞信号码
     * @param type $phone
     * @return type 
     */
    function getUserId($phone='') {
        $post_data  = array('searchText'=>$phone);
        $ch = curl_init();
        $url    = 'http://f.10086.cn/im/index/searchOtherInfoList.action;jsessionid='.$this->jsessionid.'?t='.$this->t;
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //这行是设定curl是否跟随header发送的location, 重要
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_REFERER, 'http://f.10086.cn/im/index/index.action;jsessionid='.$this->jsessionid.'?t='.$this->t);
        curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        $return = curl_exec($ch);
        curl_close($ch);
        
        preg_match('~touserid=(.*?)&~', $return, $id);
        if(array_key_exists(1, $id)) {
            return $id[1];
        }else{
            return 0;
        }
        
    }

    //好友列表
    function goHomePage(){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'http://f.10086.cn'.$this->login['go_url']);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //这行是设定curl是否跟随header发送的location, 重要
        curl_setopt($ch, CURLOPT_POST, 0);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_REFERER, $this->login['url']);
        curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        $return = curl_exec($ch);
        curl_close($ch);
        return $return;
    }
    
    /**
     * 发送信息
     * @param type $userid
     * @param type $msg
     * @return type 
     */
    function sendMsg($userid, $msg) {
        if(mb_check_encoding($msg, 'gb2312')) {
            $msg    = mb_convert_encoding($msg, 'utf-8', 'gb2312');
        }elseif(mb_check_encoding($msg, 'gbk')) {
            $msg    = mb_convert_encoding($msg, 'utf-8', 'gbk');
        }
        $from   = 'http://f.10086.cn/im/index/index.action'.$this->fix;
        $url    = 'http://f.10086.cn/im/chat/sendMsg.action'.$this->fix.'&touserid='.$userid;

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //这行是设定curl是否跟随header发送的location, 重要
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_REFERER, $from);
        curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);
        //curl_setopt($ch, "Connection", "Keep-Alive");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "msg=$msg");
        $return = curl_exec($ch);
        curl_close($ch);
        preg_match('~<p>(.*?)<a href~s', $return, $s);
        $msg    = trim($s[1]);
        return $msg;
    }
    
    public function getLogin() {
        return $this->login;
    }

    //退出
    function logout() {
        $url    = 'http://f.10086.cn/im/index/logoutsubmit.action?t='.$this->t;
    }
}

$user   = $_GET['user'];
$pwd    = $_GET['pwd'];
$type   = empty ($_GET['type'])?4:$_GET['type'];
$msg    = $_GET['msg'];
$userid = $_GET['userid'];

if(!empty($user) && !empty($pwd) && !empty($type) && !empty($msg) &&  is_numeric($userid) ) {
    $fetion = new Fetion($user, $pwd, $type);
    $home   = $fetion->goHomePage();
    $phone  = $fetion->getUserId($userid);
    //print_r($fetion->getUserId($userid));
    $send   = $fetion->sendMsg($phone, $msg);
    $fetion->logout();
    echo $send;
}else{
    echo '缺少参数';
}
?>
